<template>
	<view class="login u-flex u-col-center u-row-center">
		<u-navbar title-size="32" :border-bottom="false"></u-navbar>
		<view class="">
			<u-button :custom-style="{width:'700rpx',height:'80rpx',background:'#F5B340',color:'#FFFFFF'}" open-type='getUserInfo'
			 @getuserinfo='login' v-if="!isGetPhone">微信一键登录</u-button>
			<u-button :custom-style="{width:'700rpx',height:'70rpx',background:'#F5B340',color:'#FFFFFF'}" open-type="getPhoneNumber"
			 @getphonenumber="getPhoneNumber" v-if="isGetPhone">微信手机号快捷登录</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	let code = "";
	export default {
		data() {
			return {
				isGetPhone:false
			}
		},
		onReady() {
			this.getWXCode().then(res => {
				code = res.code
			})
		},
		methods: {
			login() {
				uni.checkSession({
					success: (res) => {
						console.log(res)
						if (res.errMsg === "checkSession:ok") {
							// 获取用户信息
							this.getUserInfo()
						} else {
							this.getWXCode().then(res => {
								if (res.errMsg === "login:ok") {
									code = res.code
									this.getUserInfo()
								} else {
									uni.showToast({
										mask: true,
										title: res.errMsg
									})
								}
							}).catch(err=>{
								uni.showModal({
									title:"系统提示",
									content:err
								})
							})
						}

					},
					fail: (err) => {
						this.getWXCode().then(res => {
							if (res.errMsg === "login:ok") {
								code = res.code
								this.getUserInfo()
							} else {
								uni.showToast({
									mask: true,
									title: res.errMsg
								})
							}
						}).catch(err=>{
								uni.showModal({
									title:"系统提示",
									content:err
								})
							})
						console.log(err)
					}
				})

			},
			getPhoneNumber(e) {
				console.log(e)
				if(e.detail.errMsg === "getPhoneNumber:ok"){
					uni.showLoading({
						mask: true,
						title: '登录中...'
					})
					this.getWXCode().then(res => {
						let params = {
							code: res.code,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							// inviter_id: this.vuex_shareid
						}
						this.$u.api.wxMobileLogin(params).then(res => {
							uni.hideLoading()
							console.log(res)
							if (res.errno === "000") {
								// uni.setStorageSync('info', res.result.data)
								this.$u.vuex('vuex_user', res.result.data);
								this.$u.vuex('vuex_token', res.result.data.auth);
								this.$u.mpShare.path = `/pages/index/index?member_id=${this.vuex_user.id}`
								uni.navigateBack({})
							} else {
								this.$refs.uToast.show({
									title: res.errmsg,
									type: 'error',
								})
							}
						})
					})
				}
			},
			getUserInfo() {
				uni.getSetting({
					success: (res) => {
						console.log(res)
						if(res.authSetting['scope.userInfo']){
							uni.showLoading({
								mask: true,
								title: '用户信息授权中'
							})
							uni.getUserInfo({
								provider: 'weixin',
								success: (infoRes) => {
									console.log('用户昵称为：', infoRes);
									// uni.hideLoading()
									if (infoRes.errMsg === "getUserInfo:ok") {
										let params = {
											code: code,
											encryptedData: infoRes.encryptedData,
											iv: infoRes.iv,
											member_id:this.vuex_shareid
										}
										this.$u.api.wxLogin(params).then(res => {
											uni.hideLoading()
											console.log(res)
											this.isGetPhone = true
											if (res.errno === "000") {
												// uni.setStorageSync('info', res.result.data)
												// this.$u.vuex('vuex_user', res.result.data);
												// this.$u.vuex('vuex_token', res.result.data.auth);
												this.$u.mpShare.path = `/pages/index/index?member_id=${this.vuex_user.id}`
												// uni.navigateBack({})
											} else {
												this.$refs.uToast.show({
													title: res.errmsg,
													type: 'error',
												})
											}
										})
									}
								},
								fail: (err) => {
									uni.hideLoading()
									uni.showToast({
										mask: true,
										title: err
									})
								}
							});
						}
					}
				})
				
			},
			getWXCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes);
							if (loginRes.errMsg === "login:ok") {
								resolve(loginRes)
							} else {
								reject(loginRes.errMsg)
							}
						}
					});
				})
			}
		}
	}
</script>

<style>
	.login {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
	}
</style>
